<%@ page import="java.nio.charset.StandardCharsets" %>
<%@ page import="wtc.stu.entity.StuEntity" %>
<%@ page import="wtc.stu.dao.StuDaoGrade" %><%--
  Created by IntelliJ IDEA.
  User: 马可
  Date: 2022/10/27
  Time: 10:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    try {
%>
<%
    try {
%>
<%--增加年级信息--%>
<%
    //从form表单获取数据
    String gradeName = new String(request.getParameter("gradeName").getBytes("ISO8859_1"), StandardCharsets.UTF_8);
    String gradeRemark = new String(request.getParameter("gradeRemark").getBytes("ISO8859_1"), StandardCharsets.UTF_8);
    int gid = Integer.parseInt(request.getParameter("gid"));
    // 使用set方法将获取到的数据进行存储并封装
    StuEntity grade = new StuEntity();
    grade.setGid(gid);
    grade.setGname(gradeName);
    grade.setGremark(gradeRemark);
    // 调用相应类的addStu方法将封装好的数据作为参数发送到数据库
    StuDaoGrade daoGrade = new StuDaoGrade();
    daoGrade.addStu(grade);
%>
<%--增加成功提示信息--%>
<script type="text/javascript">
    alert("增加成功！");
    // 跳转到指定页面
    window.location.href = "grade.jsp";
</script>
<%
} catch (Exception e) {
%>
<%--修改年级信息--%>
<%
    // 从form表单获取数据
    String newGradeName = new String(request.getParameter("newGradeName").getBytes("ISO8859_1"), StandardCharsets.UTF_8);
    String newGradeRemark = new String(request.getParameter("newGradeRemark").getBytes("ISO8859_1"), StandardCharsets.UTF_8);
    int newGid = Integer.parseInt(request.getParameter("newGid"));
    // 使用set方法将获取到的数据进行存储并封装
    StuEntity gradeUpdate = new StuEntity();
    gradeUpdate.setGid(newGid);
    gradeUpdate.setGname(newGradeName);
    gradeUpdate.setGremark(newGradeRemark);
    // 调用相应类的addStu方法将封装好的数据作为参数发送到数据库
    StuDaoGrade daoGradeUpdate = new StuDaoGrade();
    daoGradeUpdate.updateStu(gradeUpdate);
%>
<%--修改成功提示信息--%>
<script type="text/javascript">
    alert("修改成功！");
    // 跳转到指定页面
    window.location.href = "grade.jsp";
</script>
<%
    }
%>
<%
} catch (Exception e) {
%>
<%--删除年级信息--%>
<%
    // 从form表单获取数据
    int delGid = Integer.parseInt(request.getParameter("delGid"));
    // 调用相应类的方法，并将获取到的数据作为参数
    StuDaoGrade daoGradeDel = new StuDaoGrade();
    daoGradeDel.delStu(delGid);
    // 再次通过form表单获取到的数据调用相应类的getOneById方法进行查询，用返回值来进行判断
    StuEntity pd = daoGradeDel.getOneById(delGid);
    int c = pd.getGid();
    if (c == 0) {
%>
<%--删除成功提示信息--%>
<script type="text/javascript">
    alert("删除成功！");
    // 跳转到指定页面
    window.location.href = "grade.jsp";
</script>
<%
} else {
%>
<%--删除失败提示信息--%>
<script type="text/javascript">
    alert("删除失败！存在外键关联或其他原因！");
    // 跳转到指定页面
    window.location.href = "grade.jsp";
</script>
<%
    }
%>
<%
    }
%>
</body>
</html>
